from allpairspy import AllPairs

from api_py.enums.cookie import Cookie
from api_py.generate_case.template import template_case
from api_py.utils.file_sys import write_json

user = [
    {"value": "ff80818184cdbf5e0184d7ff1be70164", "description": "合法督导id"},
    {"value": "invalid_id", "description": "不合法用户"},
]

weekDays = [
    {"value": [1, 2, 3, 4, 5], "description": "合法排班周"},
]

userType = [
    {"value": "COUNSELOR", "description": "咨询师排班"},
    {"value": "SUPERVISOR", "description": "督导排班"},
    {"value": "INVALID_USER", "description": "非法用户"},
]

cookies = [
    {"value": Cookie.ADMIN_COOKIE.value, "description": "管理员登录"},
    {"value": Cookie.SUPERVISOR_COOKIE.value, "description": "督导登录"},
    {"value": Cookie.COUNSELOR_COOKIE.value, "description": "咨询师登录"},
    {"value": Cookie.WEIXIN_COOKIE.value, "description": "微信访客登录"},
    {"value": Cookie.NULL_COOKIE.value, "description": "未登录"},
    {"value": Cookie.ERROR_COOKIE.value, "description": "其他Cookie"},
]

data = [
    {"value": "ff80818184cdbf5e0184d7ff1be70164", "description": "合法督导id"},
    {"value": [1, 2, 3, 4, 5], "description": "合法排班"},
    {"value": "SUPERVISOR", "description": "督导排班"},
]

# 插入非法排班周，会引起系统崩溃
invalid_week = [
    {"value": "ff80818184cdbf5e0184d7ff1be70164", "description": "合法督导id"},
    {"value": ["abc", "week"], "description": "非法排班周"},\
    {"value": "SUPERVISOR", "description": "督导排班"},
]

def case23_arrange_batch(api_type):
    json_result = {}
    parameters = [user, weekDays, userType]
    i = 0

    # 检测数据格式的用例
    for pairs in AllPairs(parameters):
        case = template_case()
        request = case["request"]
        # 设置url参数
        wrap_data(request, pairs)
        # 设置请求头cookie
        request["headers"]["Cookie"] = cookies[0]

        json_result[i] = case
        i += 1

    # 检测用户权限用例
    for cookie in cookies:
        case = template_case()
        request = case["request"]
        # 设置url参数
        wrap_data(request, data)
        # 设置请求头cookie
        request["headers"]["Cookie"] = cookie

        json_result[i] = case
        i += 1

    # 非法排版周用例
    case = template_case()
    request = case["request"]
    # 设置url参数
    wrap_data(request, invalid_week)
    # 设置请求头cookie
    request["headers"]["Cookie"] = cookies[0]

    json_result[i] = case

    write_json(api_type.value[0]["file_name"], json_result)

# 打包用例请求参数
def wrap_data(request, data):
    request["body"]["user"] = data[0]
    request["body"]["weekDays"] = data[1]
    request["body"]["userType"] = data[2]